from pydantic import BaseModel, Field, validator
from typing import Optional


class FacebookInfoRequest(BaseModel):
    """获取Facebook视频信息请求DTO"""
    facebook_url: str = Field(..., description="Facebook视频链接", min_length=1, max_length=2048)
    
    @validator('facebook_url')
    def validate_facebook_url(cls, v):
        """验证Facebook URL格式"""
        if not v or not v.strip():
            raise ValueError('Facebook链接不能为空')
        
        v = v.strip()
        
        # 简单的Facebook URL格式验证
        valid_domains = ['facebook.com', 'www.facebook.com', 'm.facebook.com', 'fb.com', 'fb.watch']
        
        if not any(domain in v for domain in valid_domains):
            raise ValueError('请提供有效的Facebook链接')
        
        if not (v.startswith('http://') or v.startswith('https://')):
            raise ValueError('链接格式不正确，必须以http://或https://开头')
        
        # 检查是否包含视频相关路径
        video_patterns = ['/watch/', '/videos/', '/posts/', '/reel/']
        if not any(pattern in v for pattern in video_patterns):
            raise ValueError('链接必须包含视频内容（/watch/、/videos/、/posts/或/reel/）')
        
        return v


class FacebookUrlRequest(BaseModel):
    """获取Facebook下载链接请求DTO"""
    facebook_url: str = Field(..., description="Facebook视频链接", min_length=1, max_length=2048)
    quality: Optional[str] = Field("best", description="视频质量", max_length=10)
    
    @validator('facebook_url')
    def validate_facebook_url(cls, v):
        """验证Facebook URL格式"""
        if not v or not v.strip():
            raise ValueError('Facebook链接不能为空')
        
        v = v.strip()
        
        # 简单的Facebook URL格式验证
        valid_domains = ['facebook.com', 'www.facebook.com', 'm.facebook.com', 'fb.com', 'fb.watch']
        
        if not any(domain in v for domain in valid_domains):
            raise ValueError('请提供有效的Facebook链接')
        
        if not (v.startswith('http://') or v.startswith('https://')):
            raise ValueError('链接格式不正确，必须以http://或https://开头')
        
        # 检查是否包含视频相关路径
        video_patterns = ['/watch/', '/videos/', '/posts/', '/reel/']
        if not any(pattern in v for pattern in video_patterns):
            raise ValueError('链接必须包含视频内容（/watch/、/videos/、/posts/或/reel/）')
        
        return v
    
    @validator('quality')
    def validate_quality(cls, v):
        """验证视频质量"""
        if v:
            valid_qualities = ['360p', '480p', '720p', '1080p', 'best', 'worst']
            if v.lower() not in [q.lower() for q in valid_qualities]:
                raise ValueError(f'无效的视频质量，支持的质量: {", ".join(valid_qualities)}')
            return v.lower()
        return "best"  # 默认最佳质量


class FacebookMultiQualityRequest(BaseModel):
    """获取Facebook多质量下载链接请求DTO"""
    facebook_url: str = Field(..., description="Facebook视频链接", min_length=1, max_length=2048)
    
    @validator('facebook_url')
    def validate_facebook_url(cls, v):
        """验证Facebook URL格式"""
        if not v or not v.strip():
            raise ValueError('Facebook链接不能为空')
        
        v = v.strip()
        
        # 简单的Facebook URL格式验证
        valid_domains = ['facebook.com', 'www.facebook.com', 'm.facebook.com', 'fb.com', 'fb.watch']
        
        if not any(domain in v for domain in valid_domains):
            raise ValueError('请提供有效的Facebook链接')
        
        if not (v.startswith('http://') or v.startswith('https://')):
            raise ValueError('链接格式不正确，必须以http://或https://开头')
        
        # 检查是否包含视频相关路径
        video_patterns = ['/watch/', '/videos/', '/posts/', '/reel/']
        if not any(pattern in v for pattern in video_patterns):
            raise ValueError('链接必须包含视频内容（/watch/、/videos/、/posts/或/reel/）')
        
        return v 